// 仓库
import { reqgetcate,reqgetgoods } from '../../http/api'
//State
const initState = {
    getcate: [],
    getgoods: [],
    n:0

}
//action type 
const TYPES = {
    HOME_CHANGE_CATE: "HOME_CHANGE_CATE",
    HOME_CHANGE_GOODS: "HOME_CHANGE_GOODS",
    HOME_CHANGE_N:"HOME_CHANGE_N"
}
//reducer
const reducer = (state = initState, action) => {
    switch (action.type) {
        case TYPES.HOME_CHANGE_CATE:
            return {
                ...state,
                getcate: action.getcate
            }
        case TYPES.HOME_CHANGE_GOODS:
            return {
                ...state,
                getgoods: action.getgoods
            }
        case TYPES.HOME_CHANGE_N:
            return {
                ...state,
                n:action.n
            }  
        default:
            return state;
    }
}
//action creator
export const actions = {
    changeCate: getcate => ({ type: TYPES.HOME_CHANGE_CATE, getcate }),
    // 请求getcate
    reqCate: () => (dispatch, getState) => {
        reqgetcate().then(res => {
            // 修改getcate
            dispatch(actions.changeCate(res.data.list))
        })
    },
    // 触发getgoods
    changeGoods: getgoods => ({ type: TYPES.HOME_CHANGE_GOODS, getgoods }),
    //  请求getgoods
    reqGoods: (id) => (dispatch, getState) => {
        reqgetgoods({fid:id}).then(res => {
            //getgoods
            dispatch(actions.changeGoods(res.data.list))
        })
    },
     //   触发n的action
     changeN: n => ({ type: TYPES.HOME_CHANGE_N, n }),
}

//selector导出数据给组件
export const getCate = state => state.cate.getcate
export const getGoods=state=>state.cate.getgoods
export const getN=state=>state.cate.n
//导出
export default reducer;